package io.renren.modules.table.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.table.service.ContractService;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-15 10:33:34
 */
@Controller
@RequestMapping("contractTable")
public class ContractController extends AbstractController {

	@Autowired
	private ContractService contractService;

	/**
	 * 合同汇总
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping("/contract")
	@ResponseBody
	public R contract(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		R r = contractService.contract(params);
		return r;
	}

	@RequestMapping("/initialization")
	@ResponseBody
	public R cinitialization(@RequestParam Map<String, Object> params) {
		return contractService.contractDetailInit(params);
	}

	@RequestMapping("/contractDetail")
	@ResponseBody
	public R contractDetail(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		Map<String, Object> param = new HashMap<>();
		param = params;
		List<Map<String, Object>> chinaJoy = contractService.contractDetail(query);

		int total = contractService.contractDetailTotal(param);
		PageUtils pageUtil = new PageUtils(chinaJoy, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/contractExport")

	public void contractExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {

		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String("合同汇总报表".getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			// 查询列表数据
			R r = contractService.contract(params);
			List<Object> chinaJoy = (List<Object>) r.get("list");
			contractService.contractExport("合同汇总报表", chinaJoy, out);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping("/contractDetailExport")
	public void contractDetailExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {

		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String("合同明细报表".getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			// 查询列表数据

			contractService.contractDetailExport("合同明细报表", params, out);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
